保护成员在派生类(即子类)中是可访问的
friend funtion is not belong to any class,and may access any nember of the class which it make firends.
inline function: all the funtion defined in the class is default inline function .
the compiler cooperate the inline function like #define
this pointer is the implicitly arguement of all the menber function.
in any menber funtion ,you can use the this pointer to call the object itself.
pointer to class is like the pointer to struct ,which can access the member object.
static menber and static mener function has the only copy int the class,
any object of this class not have a copy.but it`s accessible to any object of this class.
inheritance:
class classname: access-specifier base-class
derived-class (public,private,protected) defined class
Access public protected private
identical-class yes yes yes
inherient-class yes yes no
outer-class yes no no
A derived class inherits all base class methods with the following exceptions:
- Constructors, destructors and copy constructors of the base class.
- Overloaded operators of the base class.
- The friend functions of the base class.
``
Type of Inheritance:
When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:
``
Multiple Inheritances:
class derived-class: access baseA, access baseB….
Overloading function and Overloading operator
int the same scope,you can declare some function with the same name,but the parameter list must be different. when the function called ,the compiler will select the most approriate overloaded funtion or operator, which is called overloaded resolution.
overloaded funtion character:
The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
the list of operators, which can not be overloaded: :: .* . ?;
1234567891011 friend ostream &operator<<( ostream &output, const Distance &D ){output << "F : " << D.feet << " I : " << D.inches;return output;}friend istream &operator>>( istream &input, Distance &D ){input >> D.feet >> D.inches;return input;}
Polymorphism
polymorphism occurs when there is hierarchy of class and they are related by inheritance.
polymorphism in c++ means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
the call of the member function is being set once by the compiler as the version defined in the base class,which is called static resolution of the function call, or static linkage and also sometimes called early binding
Virtual funtion precede the declaration of menber funtion() in the base class with the keyword virtual.
virtualed in the base class and each of the child classes has a separate implementation for the function(). This is how polymorphism is generally used.A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.
What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as **dynamic linkage**, or **late binding**.
Pure Virtual Functions:
there is no meaningful definition you could give for the function in the base class.
virtual type functionname() = 0;
The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.
c++ inteface are implemented by Abstract class
A class is made abstract by declaring at least one of its functions as pure virtual function. using virtual
and placing “=0 “ in its declaration.
The purpose of an abstract class (often referred to as an ABC) is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface.
if a subclass of an ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC
Classes that can be used to instantiate objects are called concrete classes
.
Memory in c++ program ia divided into two parts:
|
|
double* pvalue = NULL;
if( !(pvalue = new double ))
{
cout << “Error: out of memory.” <
int ROW = 2;
int COL = 3;
double *pvalue = new double [ROW]; // Allocate memory for rows
// Now allocate memory for columns
for(int i = 0; i < COL; i++) {
pvalue[i] = new double[COL];
}
for(int i = 0; i < COL; i++) {
delete[] pvalue[i];
}
delete [] pvalue;
template
{
// body of function
}
```
CGI